﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QUANLYNHASACH
{
    public partial class frmSACH : Form
    {
        public frmSACH()
        {
            InitializeComponent();
        }
        QUANLYNHASACH.App_code.BLL.SACH_BLL sach_bll = new QUANLYNHASACH.App_code.BLL.SACH_BLL();
        private void frmSACH_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'ds_quanlybansach.NHAXUATBAN' table. You can move, or remove it, as needed.
            //this.nHAXUATBANTableAdapter.Fill(this.ds_quanlybansach.NHAXUATBAN);
            // TODO: This line of code loads data into the 'ds_quanlybansach.THELOAI' table. You can move, or remove it, as needed.
            //this.tHELOAITableAdapter.Fill(this.ds_quanlybansach.THELOAI);
            dtgSach.DataSource = sach_bll.LayChiTietSach();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {

                string idsach = "000" + txtID.Text;
                string idtheloai = cbTheLoai.SelectedValue.ToString();
                string idnxb = cbNXB.SelectedValue.ToString();
                string tensach = txtTenSach.Text;
                string tacgia = txtTacGia.Text;
                string dichgia = txtDichGia.Text;
                float kichthuoc = float.Parse(txtKichThuoc.Text);
                string hinhthuc = txtHinhThuc.Text;
                float trongluong = float.Parse(txtTrongLuong.Text);
                float giaban = float.Parse(txtGiaBan.Text);
                int SLTrongKho = int.Parse(txtSLTrongKho.Text);
                int sotrang = int.Parse(txtSoTrang.Text);
                
                sach_bll.ThemSach(idsach, idtheloai, idnxb, tensach, tacgia, dichgia, sotrang, kichthuoc, hinhthuc, trongluong, giaban, SLTrongKho);
                MessageBox.Show("Thành công!", "thông báo");
                dtgSach.DataSource = sach_bll.LayChiTietSach();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool isCheck = false;
                for (int i = 0; i < dtgSach.Rows.Count; i++)
                {

                    if (this.dtgSach.Rows[i].Cells[0].Value != null)
                    {
                        isCheck = (bool)this.dtgSach.Rows[i].Cells[0].Value;
                        string idSach = this.dtgSach.Rows[i].Cells[1].Value.ToString();
                        int SLTrongKho = int.Parse(this.dtgSach.Rows[i].Cells[12].Value.ToString());
                        if (SLTrongKho == 0)
                        {
                            if (isCheck == true)
                            {
                                sach_bll.xoasach(idSach);

                            }
                        }
                        else
                        {
                            MessageBox.Show("Số lượng còn trong kho >0, không thể xóa sách này", "Thông báo");
                        }
                    }
                }
                MessageBox.Show("Oh yeah!", "thông báo");
                dtgSach.DataSource = sach_bll.LayChiTietSach();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                bool isCheck = false;
                for (int i = 0; i < this.dtgSach.Rows.Count; i++)
                {

                    if (this.dtgSach.Rows[i].Cells[0].Value != null)
                    {
                        isCheck = (bool)this.dtgSach.Rows[i].Cells[0].Value;
                        string idSach = this.dtgSach.Rows[i].Cells[1].Value.ToString();
                        string idTheLoai_new = this.dtgSach.Rows[i].Cells[2].Value.ToString();
                        string idNhaXuatBan_new = this.dtgSach.Rows[i].Cells[3].Value.ToString();
                        string TenSach_new = this.dtgSach.Rows[i].Cells[4].Value.ToString();
                        string TacGia_new = this.dtgSach.Rows[i].Cells[5].Value.ToString();
                        string DichGia_new = this.dtgSach.Rows[i].Cells[6].Value.ToString();
                        int SoTrang_new = int.Parse(this.dtgSach.Rows[i].Cells[7].Value.ToString());
                        float KichThuoc_new = float.Parse(this.dtgSach.Rows[i].Cells[8].Value.ToString());
                        string HinhThuc_new = this.dtgSach.Rows[i].Cells[9].Value.ToString();
                        float TrongLuong_new = float.Parse(this.dtgSach.Rows[i].Cells[10].Value.ToString());
                        float GiaBia_new = float.Parse(this.dtgSach.Rows[i].Cells[11].Value.ToString());
                        int SLTrongKho = int.Parse(this.dtgSach.Rows[i].Cells[12].Value.ToString());
                        if (isCheck == true)
                        {
                            
                            sach_bll.suasach(idSach, idTheLoai_new, idNhaXuatBan_new, TenSach_new, TacGia_new, DichGia_new, SoTrang_new, KichThuoc_new, HinhThuc_new, TrongLuong_new, GiaBia_new, SLTrongKho);


                        }
                    }
                }
                MessageBox.Show("Thành công!", "thông báo");
                this.dtgSach.DataSource = sach_bll.LayChiTietSach();


            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
    }
}
